/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.movieSystem.view;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;

import javax.swing.*;

import com.movieSystem.model.UserInfo;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import tcpFirmware.OKClient;
import tcpFirmware.Response;

/**
 *
 * @author DELL-PC
 */
public class FrmChange extends javax.swing.JFrame {
	String username;
	FrmUser user;
    /**
     * Creates new form FrmChange
     */
    public FrmChange(FrmUser user) {
        initComponents();
        this.username= UserInfo.getUsername();
        this.user=user;
        this.setAlwaysOnTop(true);
    }


	public void eit(){
        this.dispose();
        user.dispose();
        Platform.runLater(()->user.movieTime.close());
        this.setAlwaysOnTop(false);
        new FrmLogin().setVisible(true);
    }
	
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LabelOldPassword = new javax.swing.JLabel();
        LabelNewPassword = new javax.swing.JLabel();
        LabelConfirmPassword = new javax.swing.JLabel();
        FieldOldPassword = new javax.swing.JPasswordField();
        FieldNewPassword = new javax.swing.JPasswordField();
        FieldConfirmPassword = new javax.swing.JPasswordField();
        BtnConfirm = new javax.swing.JButton();
        BtnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("修改密码");

        LabelOldPassword.setText("旧  密  码：");

        LabelNewPassword.setText("新  密  码：");

        LabelConfirmPassword.setText("确认新密码：");

        FieldOldPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FieldOldPasswordActionPerformed(evt);
            }
        });

        FieldNewPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FieldNewPasswordActionPerformed(evt);
            }
        });

        FieldConfirmPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FieldConfirmPasswordActionPerformed(evt);
            }
        });

        BtnConfirm.setText("确认修改");
        BtnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnConfirmActionPerformed(evt);
            }
        });

        BtnCancel.setText("取消修改");
        BtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BtnConfirm)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                        .addComponent(BtnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(LabelConfirmPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(LabelNewPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(LabelOldPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(FieldOldPassword)
                            .addComponent(FieldNewPassword)
                            .addComponent(FieldConfirmPassword))))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelOldPassword)
                    .addComponent(FieldOldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelNewPassword)
                    .addComponent(FieldNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelConfirmPassword)
                    .addComponent(FieldConfirmPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnConfirm)
                    .addComponent(BtnCancel))
                .addContainerGap(49, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void FieldOldPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FieldOldPasswordActionPerformed
    	//获取旧密码并创建字符串
    	char[] opw=FieldOldPassword.getPassword();
        String oldpassword=new String(opw);
        
    }//GEN-LAST:event_FieldOldPasswordActionPerformed

    private void FieldNewPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FieldNewPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_FieldNewPasswordActionPerformed

    private void FieldConfirmPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FieldConfirmPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_FieldConfirmPasswordActionPerformed

    private void alert(String text){
        JOptionPane.showMessageDialog(this,text,"系统提示",JOptionPane.INFORMATION_MESSAGE);
    }
    private void BtnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnConfirmActionPerformed
    	if (!Arrays.equals(FieldNewPassword.getPassword(), FieldConfirmPassword.getPassword())){
            JOptionPane.showMessageDialog(this,"两个密码不一致","系统提示",JOptionPane.INFORMATION_MESSAGE);
            return;
    	}

        OKClient okClient;
		try {
			okClient = new OKClient(new Response() {
				public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
					// TODO Auto-generated method stub
					HashMap<String, String> data = OKClient.dealWithForm(dataBytes);
					if(header.get("修改状态").equals("修改成功")) {
						alert("密码修改成功");
						eit();
					}else {
					    alert("密码修改失败");
					}
			}});
			okClient.setType("text/formData").setRoute("change").setData("username", username).setData("oldPassword",new String(FieldOldPassword.getPassword()) )
                    .setData("newPassword",new String(FieldNewPassword.getPassword()));
			okClient.sendTo();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }//GEN-LAST:event_BtnConfirmActionPerformed

    private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_BtnCancelActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnCancel;
    private javax.swing.JButton BtnConfirm;
    private javax.swing.JPasswordField FieldConfirmPassword;
    private javax.swing.JPasswordField FieldNewPassword;
    private javax.swing.JPasswordField FieldOldPassword;
    private javax.swing.JLabel LabelConfirmPassword;
    private javax.swing.JLabel LabelNewPassword;
    private javax.swing.JLabel LabelOldPassword;
    // End of variables declaration//GEN-END:variables
}
